/**
 * 模型获取请求队列工具类
 *
 * @description 避免模型获取请求频繁发送
 * @author chitanda
 * @date 2022-06-24 11:06:38
 * @export
 * @class ModelQueue
 */
export class ModelQueue {
  protected static queue: Map<string, any[]> = new Map();

  /**
   * 是否已存在队列
   *
   * @author chitanda
   * @date 2022-06-24 11:06:31
   * @static
   * @param {string} tag 队列标识
   * @return {*}  {boolean}
   */
  static isQueue(tag: string): boolean {
    if (this.queue.has(tag)) {
      return true;
    }
    return false;
  }

  /**
   * 添加至队列
   *
   * @author chitanda
   * @date 2022-06-24 11:06:57
   * @static
   * @param {string} tag 队列标识
   * @param {(result: unknown) => void} resolve 队列执行完毕回调
   */
  static add(tag: string, resolve: (result: unknown) => void): void {
    if (this.isQueue(tag)) {
      this.queue.get(tag).push(resolve);
    } else {
      this.queue.set(tag, [resolve]);
    }
  }

  /**
   * 队列结束
   *
   * @author chitanda
   * @date 2022-06-24 13:06:27
   * @static
   * @param {string} tag 队列标识
   * @param {unknown} result 队列执行结果
   */
  static done(tag: string, result: unknown): void {
    if (this.isQueue(tag)) {
      this.queue.get(tag).forEach(resolve => {
        resolve(result);
      });
      this.queue.delete(tag);
    }
  }
}
